<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
$param = false;
$file = false;
if (
  (isset($_POST['wx']) &&
    isset($_POST['wx']['audioUrl']) && is_string($_POST['wx']['audioUrl']) &&
    isset($_POST['wx']['param']['request']) && is_array(($_POST['wx']['param']['request'])) &&
    isset($_POST['wx']['param']['audio']) && is_array(($_POST['wx']['param']['audio'])))
) {
  $param = $_POST['wx']['param'];
  $param['audio'] = [
    'audioType' => $param['audio']['audioType'],
    'sampleRate' => (int) $param['audio']['sampleRate'],
    'channel' => (int) $param['audio']['channel'],
    'sampleBytes' => (int) $param['audio']['sampleBytes'],
  ];
  $file = curl_https($_POST['wx']['audioUrl']);
} elseif (
  (isset($_POST['h5']) &&
    isset($_POST['h5']['file']) && is_string($_POST['h5']['file']) &&
    isset($_POST['h5']['param']['request']) && is_array(($_POST['h5']['param']['request'])) &&
    isset($_POST['h5']['param']['audio']) && is_array(($_POST['h5']['param']['audio'])))
) {
  $param = $_POST['h5']['param'];
  $param['audio'] = [
    'audioType' => $param['audio']['audioType'],
    'sampleRate' => (int) $param['audio']['sampleRate'],
    'channel' => (int) $param['audio']['channel'],
    'sampleBytes' => (int) $param['audio']['sampleBytes'],
  ];
  $file = fopen($_POST['h5']['file'], 'r');
}
if (!$param || !$file) die;
if (!$param['userId']) $param['userId'] = "test";



//---------------------引入依赖包---------------------------------------
require 'vendor/autoload.php';

use SSPHPSDK\RequestValidator;
use Symfony\Component\Dotenv\Dotenv;
use SSPHPSDK\SSTool\Upstream;

//---------------------基础变量配置，请根据.env.dist 文件配置.env文件-------------------------------------
define('ROOT_PATH', dirname(__FILE__));

if (file_exists(ROOT_PATH . '/.env')) {
  $dotenv = new Dotenv();
  $dotenv->load(ROOT_PATH . '/.env');

  if (getenv('SINGSOUND_WARRANT_RUNTIME_ENV')) {
    define('SINGSOUND_WARRANT_RUNTIME_ENV', getenv('SINGSOUND_WARRANT_RUNTIME_ENV'));
    if (SINGSOUND_WARRANT_RUNTIME_ENV == "prod") {
      error_reporting(0);
    }
  }
} else {
  die("env config file not found");
}


//---------------------第一步：授权申请：变量配置-------------------------------------
//用户id；请确保用户id真实有效，不要使用固定字符串；用来跟踪用户行为，提高学习效果，防火墙基础参数
$user_id = $param['userId'];

// 用户信息；包含用户id、用户的客户端IP。
$user_data = array(
  "user_id" => $user_id,
  "user_client_ip" => $_SERVER["REMOTE_ADDR"], //只有cli模式下才需要显式输入ip
  "dummy_info" => "content" //想要传入的其他内容
);
// mPrintX($user_data);
//---------------------第一步：授权申请：请求授权-------------------------------------
$warrant_info = RequestValidator::getWarrantWithUserInfo($user_data);
// mPrintX($warrant_info);

//得到授权ID，这里请加入更多的错误判断
if (array_key_exists("warrant_id", $warrant_info["data"])) {
  $warrant_id = $warrant_info["data"]["warrant_id"];
} else {
  // var_dump($warrant_info);
  die("没有找到 warrant_id");
}
// mPrintX($warrant_info);

//---------------------第二步：语音评测：参数配置 文本部分-------------------------------------
//内核类型
$core_type = $param['request']['coreType'] ? $param['request']['coreType'] : "en.sent.score"; // param.request.coreType
// mPrintX($core_type);
$ap = true;
//内核评测参数
$core_params = array(
  "refText" => $param['request']['refText'], // param.request.refText
  "precision" => $param['request']['precision'] ? $param['request']['precision'] : 1, // param.request.precision
  "rank" => $param['request']['rank'] ? $param['request']['rank'] : 100, // param.request.rank
  "tokenId" => $param['request']['tokenId'] ? $param['request']['tokenId'] : 'default' // param.request.tokenId
);
//音频属性
// $audio_info = array(
//   "sampleBytes" => 2,
//   "sampleRate" => $ap ? 8000 : 16000,
//   "channel" => 1,
//   "audioType" => $ap ? "amr" : 'wav'
// );
$audio_info = $param['audio']; // param.audio
// mPrintX($audio_info);

//得到标准的语音评测的文本格式
$text = Upstream::buildJSONArray($user_id, $core_type, $warrant_id, $core_params, $audio_info);
// mPrintX($text);
//得到连接id
$connection_id = $text['start']['param']['app']['connect_id'];
//得到请求id
$request_id = $text['start']['param']['request']['request_id'];
//得到app_id,也可以直接从配置文件读取
$app_id = $text['start']['param']['app']['applicationId'];
$ret = $text;
//---------------------第二步：语音评测：参数配置 音频和表单部分-------------------------------------
//直接打开音频文件来提交
$form = [
  [
    'name' => 'text',
    'contents' => json_encode($text)
  ], [
    'name' => 'audio',
    // 'contents' => fopen('./test/en.word.score/request.wav', 'r')
    'contents' => $file
  ]
];
//音频内容可以用fopen，也可以直接二进制放内容
// $form = [ ['name' => 'text','contents' => json_encode($text)],[ 'name' => 'audio',  'contents' => $content]];  
// $form = [ ['name' => 'text','contents' => json_encode($text)],[ 'name' => 'audio',  'contents' => file_get_contents('./test/en.word.score/request.wav')]];
// mPrintX($text);

//---------------------第二步：语音评测：参数配置 URL部分-------------------------------------
//拼接请求地址
$url_query_string = "/$core_type?appkey=$app_id&connect_id=$connection_id&request_id=$request_id&warrant_id=$warrant_id";
// var_dump($form[0]);
// mPrintX($url_query_string);
//---------------------第二步：语音评测：发起评测请求并得到结果-------------------------------------
// 结合评测请求发起评测得到结果
try {
  $result = Upstream::proceed_ginger_eval_result($url_query_string, $form);
  // $result = curl_https(getenv('SINGSOUND_GINGER_SERVER_URL') . $url_query_string, $form);
  // mPrintX([$form]);
} catch (Exception $e) {
  print("request error,please check!");
  print $e->getMessage();
  print $e->getTraceAsString();
  die();
}
$ret = $result["result"];
// $ret['audio'] = $audio_info;
//---------------------第三步：客户业务逻辑实现-------------------------------------
// var_dump($ret);
mPrintX($ret);
